package com.ruoyi.hospital.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.hospital.domain.BaseQuery;
import com.ruoyi.hospital.domain.Dept;
import com.ruoyi.hospital.mapper.HospitalDeptMapper;
import com.ruoyi.hospital.service.IHospitalDeptService;
import com.ruoyi.hospital.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalDeptServiceImpl implements IHospitalDeptService {
    @Autowired
    private HospitalDeptMapper hospitalDeptMapper;

    @Override
    public List<Dept> findAll() {

        List<Dept> all = hospitalDeptMapper.findAll();

        return all;
    }

    @Override
    public Dept selectDeptById(Long deptId) {

        return hospitalDeptMapper.findById(deptId);
    }

    @Override
    public void update(Dept dept) {
        hospitalDeptMapper.update(dept);
    }

    /**
     * 新增
     *      -对象
     *      -根据科室查询名字，查询到对象
     *      -
     *      -parent_id的处理
     * @param dept
     */
    @Override
    public void save(Dept dept) {
        String department = dept.getDepartment();
        Dept deptTmp = selectDeptByName(department);
        /*
         *校验
         */
        if(deptTmp != null){
            Long id = deptTmp.getId();
            dept.setParentId(id);
        }
        hospitalDeptMapper.save(dept);
    }

    @Override
    public void delete(Long id) {
        hospitalDeptMapper.delete(id);
    }

    /**
     * 科室的无限极树
     * @return
     */
    @Override
    public List<Dept> queryTree() {
        // *    1.一次性查询出所有数据，放到内存 all
        List<Dept> departments = hospitalDeptMapper.findAll();
        //为了后面找自己的父亲，我们这里可以把整体的部门数据，转换一份成Map<id,department>
        //Map<Long, Dept> allDepts = departments.stream().collect(Collectors.toMap(Dept::getId, dept -> dept));
        Map<Long, Dept> allDepts = new HashMap<>();

        for (Dept department : departments) {
            //放到map中key，value对象
            allDepts.put(department.getId(), department);
        }
        List<Dept> firstDepts = new ArrayList<>();//顶级部门

        for (Dept tmp : departments) {
            if(tmp.getParentId() == null){
//        *    2.从所有数据中找出第一层级 firstDepts   我们需要返回的最外数据
                firstDepts.add(tmp);
            }else{
//        *    3.只要不是第一层级的，找自己的上级，并将自己放入上级的children中
                Dept parent = allDepts.get(tmp.getParentId());
                //需要getChildren
                if(parent != null){
                    parent.getChildren().add(tmp);
                }
            }
        }
        return firstDepts;
    }

    /**
     * 根据名字查询-科室
     * @param name
     * @return
     */
    @Override
    public Dept selectDeptByName(String name) {
        Dept dept = hospitalDeptMapper.findByName(name);
        return dept;
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @Override
    public PageList<Dept> queryPage(BaseQuery query) {
       Integer total = hospitalDeptMapper.queryCount(query);
       List<Dept> list = hospitalDeptMapper.queryData(query);

        return new PageList<Dept>(total,list);
    }


}
